//三合一。描述如何只用一个数组来实现三个栈。 
//
// 你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。
//stackNum表示栈下标，value表示压入的值。 
//
// 构造函数会传入一个stackSize参数，代表每个栈的大小。 
//
// 示例 1： 
//
// 
// 输入：
//["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
//[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
// 输出：
//[null, null, null, 1, -1, -1, true]
//说明：当栈为空时`pop, peek`返回-1，当栈满时`push`不压入元素。
// 
//
// 示例 2： 
//
// 
// 输入：
//["TripleInOne", "push", "push", "push", "pop", "pop", "pop", "peek"]
//[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
// 输出：
//[null, null, null, null, 2, 1, -1, -1]
// 
//
// 
//
// 提示： 
//
// 
// 0 <= stackNum <= 2 
// 
//
// Related Topics 栈 设计 数组 👍 80 👎 0


package LeetCode.editor.cn;

/**
 * @author ldltd
 * @date 2025-06-23 23:37:16
 * @description 面试题 03.01.三合一
 */
public class ThreeInOneLcci{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 ThreeInOneLcci fun=new ThreeInOneLcci();


	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class TripleInOne {
        int [] ar;
        int l1,l2,l3;
        int r1,r2,r3;
        int stackSize=0;
    public TripleInOne(int stackSize) {
        this.stackSize=stackSize;
        ar=new int[stackSize*3];
        l1=0;
        l2=l1+stackSize;
        l3=l2+stackSize;
        r1=l1-1;
        r2=l2-1;
        r3=l3-1;
    }
    
    public void push(int stackNum, int value) {
        if (stackNum == 0) {
            if (r1 == l1 + stackSize - 1) return;
            ar[++r1] = value;
        } else if (stackNum == 1) {
            if (r2 == l2 + stackSize - 1) return;
            ar[++r2] = value;
        } else {
            if (r3 == l3 + stackSize - 1) return;
            ar[++r3] = value;
        }
    }
    
    public int pop(int stackNum) {
        if (stackNum == 0) {
            if (r1 < l1) return -1;
            return ar[r1--];
        } else if (stackNum == 1) {
            if (r2 < l2) return -1;
            return ar[r2--];
        } else {
            if (r3 < l3) return -1;
            return ar[r3--];
        }
    }
    
    public int peek(int stackNum) {
        if (stackNum == 0) {
            if (r1 < l1) return -1;
            return ar[r1];
        } else if (stackNum == 1) {
            if (r2 < l2) return -1;
            return ar[r2];
        } else {
            if (r3 < l3) return -1;
            return ar[r3];
        }
    }
    
    public boolean isEmpty(int stackNum) {
        if (stackNum == 0) {
            return r1 < l1;
        } else if (stackNum == 1) {
            return r2 < l2;
        } else {
            return r3 < l3;
        }
    }
}

/**
 * Your TripleInOne object will be instantiated and called as such:
 * TripleInOne obj = new TripleInOne(stackSize);
 * obj.push(stackNum,value);
 * int param_2 = obj.pop(stackNum);
 * int param_3 = obj.peek(stackNum);
 * boolean param_4 = obj.isEmpty(stackNum);
 */
//leetcode submit region end(Prohibit modification and deletion)

}
